//
//  NSDate+Extensions.m
//  Cosmetic
//
//  Created by 余天龙 on 16/5/26.
//  Copyright © 2016年 YuTianLong. All rights reserved.
//

#import "NSDate+Extensions.h"

@implementation NSDate (Extensions)

static NSDateFormatter *dateFormatter;

- (NSString *)formatDateByFormatString:(NSString *)format {
    static NSDateFormatter *formatter = nil;
    if (nil == formatter) {
        formatter = [[NSDateFormatter alloc] init];
        [formatter setTimeZone:[NSTimeZone localTimeZone]];
    }
    formatter.dateFormat = format;
    NSString *str = [formatter stringFromDate:self];
    //    [formatter release];
    
    if ([str isEqualToString:@"Monday"]) {
        str = @"星期一";
    } else if ([str isEqualToString:@"Tuesday"]) {
        str = @"星期二";
    } else if ([str isEqualToString:@"Wednesday"]) {
        str = @"星期三";
    } else if ([str isEqualToString:@"Thursday"]) {
        str = @"星期四";
    } else if ([str isEqualToString:@"Friday"]) {
        str = @"星期五";
    } else if ([str isEqualToString:@"Saturday"]) {
        str = @"星期六";
    } else if ([str isEqualToString:@"Sunday"]) {
        str = @"星期日";
    }
    return str;
}

- (NSString *)formatWeek {
    
    NSString *string = [self formatDateByFormatString:@"EEEE"];
    
    return string;
}

- (NSString *)formatWeekNumber {
    NSString *string = [self formatDateByFormatString:@"EEEE"];
    NSString *str = @"";
    if ([string isEqualToString:@"星期一"]) {
        str = @"01";
    } else if ([string isEqualToString:@"星期二"]) {
        str = @"02";
    } else if ([string isEqualToString:@"星期三"]) {
        str = @"04";
    } else if ([string isEqualToString:@"星期四"]) {
        str = @"08";
    } else if ([string isEqualToString:@"星期五"]) {
        str = @"10";
    } else if ([string isEqualToString:@"星期六"]) {
        str = @"20";
    } else if ([string isEqualToString:@"星期日"]) {
        str = @"40";
    }
    return str;
}

+ (NSDate *)dateFromSystemDate:(NSDate *)date {
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    return localeDate;
}

+ (NSDateFormatter *)defaultFormatter {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dateFormatter = [[NSDateFormatter alloc]init];
    });
    return dateFormatter;
}

+ (NSDate *)dateFromString:(NSString *)timeStr
                    format:(NSString *)format {
    NSDateFormatter *dateFormatter = [NSDate defaultFormatter];
    [dateFormatter setDateFormat:format];
    NSDate *date = [dateFormatter dateFromString:timeStr];
    return date;
}

+ (NSInteger)cTimestampFromDate:(NSDate *)date {
    return (long)[date timeIntervalSince1970];
}


+(NSInteger)cTimestampFromString:(NSString *)timeStr
                          format:(NSString *)format {
    NSDate *date = [NSDate dateFromString:timeStr format:format];
    return [NSDate cTimestampFromDate:date];
}

+ (NSString *)dateStrFromCstampTime:(NSInteger)timeStamp
                     withDateFormat:(NSString *)format {

    NSTimeInterval time = timeStamp/1000;//转化为毫秒
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    return [NSDate datestrFromDate:date withDateFormat:format];
}

+ (NSString *)datestrFromDate:(NSDate *)date
               withDateFormat:(NSString *)format {
    NSDateFormatter* dateFormat = [NSDate defaultFormatter];
    [dateFormat setDateFormat:format];
    return [dateFormat stringFromDate:date];
}

@end
